Public Class VendorMaintenance

    Private _newProviders As New System.Collections.ObjectModel.Collection(Of String)
    Private _deletedProviders As New System.Collections.ObjectModel.Collection(Of String)

    Public Event VendorsChanged()

    Private Sub VendorMaintenance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.ProviderTableAdapter.FillByUserAdded(Me.Providers.Provider)

    End Sub

    Private Sub AddFigureForFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFigureForFile.Click

        If VendorName.Text.Trim.Length > 0 Then
            Dim newProvider As Providers.ProviderRow = Providers.Provider.NewProviderRow
            newProvider.provider_name = VendorName.Text.Trim
            newProvider.UserAdded = True
            Try
                Providers.Provider.AddProviderRow(newProvider)
                _newProviders.Add(VendorName.Text.Trim)
            Catch ex As Exception

            End Try
            VendorList.Refresh()
        End If

    End Sub

    Private Sub RemoveVendor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveVendor.Click

        Dim entryRow As Providers.ProviderRow
        Dim rowsToDelete As New System.Collections.ObjectModel.Collection(Of Providers.ProviderRow)

        For Each entry As System.Data.DataRowView In VendorList.SelectedItems
            entryRow = CType(entry.Row, Providers.ProviderRow)
            If entryRow.RowState = DataRowState.Added Then
                _newProviders.Remove(entryRow.provider_name)
            Else
                _deletedProviders.Add(entryRow.provider_name)
            End If
            rowsToDelete.Add(entryRow)
        Next

        For Each entryRow In rowsToDelete
            Providers.Provider.RemoveProviderRow(entryRow)
        Next

        VendorList.Refresh()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ProvidersDS.CancelEdit()
        Me.Close()

    End Sub

    Private Sub UpdateFileDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateFileDetails.Click

        Const ExpectedMessage As String = "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."

        Dim updated As Integer

        For Each deletedProvider As String In _deletedProviders
            Me.ProviderTableAdapter.DeleteQuery(deletedProvider)
        Next

        ProvidersDS.EndEdit()
        Try

            updated = Me.ProviderTableAdapter.Update(Me.Providers)


        Catch ex As OleDb.OleDbException
            If ex.Message <> ExpectedMessage Then
                MsgBox(ex.Message)
            End If
        End Try

        RaiseEvent VendorsChanged()

        Me.Close()

    End Sub

End Class